package game;

import grafica.Myframe;

import java.awt.Color;
import java.awt.Image;
import java.awt.Toolkit;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Vector;

import sprite.*;
import suono.Sound;

public class Livello implements Serializable {
	
	public static final int TILE_SIZE=50;
	Sprite[][] mappa;
	int height;
	int width;
	ArrayList<Giocatore> players;
	ArrayList<Nemico> nemici;
	int idLivello;
	private transient Sound sottofondo;

	//attributi per la lettura del file di testo
    private transient Toolkit t = Toolkit.getDefaultToolkit();;
	private transient Image imgBackground;
	private String tileset ="terra";	
	private String nomeFile;
    private List <String> listaLinee;
    private Color background;
    transient Vector<Image> uno;
    transient Vector<Image> due;
    private boolean type;
	
	public Livello(String s,ArrayList<Giocatore> players,int idLivello,boolean type) {
		this.idLivello = idLivello;
		this.players = players;
		this.type = type;
		nemici = new ArrayList<Nemico>();
		nomeFile = s;
		listaLinee = new ArrayList<String>();
		background = Color.CYAN;	
		sottofondo = new Sound();
		
		inializzaImmagini();
		leggiFile();
		creaMappa();
		//nemici.add(new Nemico(20*Livello.TILE_SIZE,50,7*Livello.TILE_SIZE,width*Livello.TILE_SIZE,pp,mappa,players.get(0)));
		//nemici.add(new Nemico(7*Livello.TILE_SIZE,50,2*Livello.TILE_SIZE,width*Livello.TILE_SIZE,pp,mappa,players.get(0)));
	}
	
	public String getNomeFile() {
		return nomeFile;
	}
	
	public void initMancanti() {
		t = Toolkit.getDefaultToolkit();
	}

	//costruttore per la rete
	public Livello(String s,int idLivello,boolean type) {
		this.type = type;
		this.idLivello = idLivello;
		this.players = new ArrayList<Giocatore>();
		nemici = new ArrayList<Nemico>();
		nomeFile = s;
		listaLinee = new ArrayList<String>();
		background = Color.CYAN;	
		sottofondo = new Sound();
		inializzaImmagini();
		leggiFile();
		creaMappa();
		//nemici.add(new Nemico(20*Livello.TILE_SIZE,50,7*Livello.TILE_SIZE,width*Livello.TILE_SIZE,pp,mappa,players.get(0)));
		//nemici.add(new Nemico(7*Livello.TILE_SIZE,50,2*Livello.TILE_SIZE,width*Livello.TILE_SIZE,pp,mappa,players.get(0)));
	}
	
	public Livello(String s,boolean type) {

		this.type = type;
		players = new ArrayList<Giocatore>();
		players.add(new Giocatore());
		nemici = new ArrayList<Nemico>();
		nomeFile = s;
		listaLinee = new ArrayList<String>();
		sottofondo = new Sound();
		inializzaImmagini();
		leggiFile();
		creaMappa();

	}
	
	public void inializzaImmagini() {
		uno = new Vector<Image>();
		uno.add(t.getImage("images/nemico1/fermo.gif"));  //0
		uno.add(t.getImage("images/nemico1/corsa1.gif"));  //1
		uno.add(t.getImage("images/nemico1/corsa2.gif"));  //2
		uno.add(t.getImage("images/nemico1/corsa3.gif"));  //3
		uno.add(t.getImage("images/nemico1/corsa2.gif"));  //4
		
		uno.add(t.getImage("images/nemico1/fermoR.gif"));  //5
		uno.add(t.getImage("images/nemico1/corsa1R.gif"));  //6
		uno.add(t.getImage("images/nemico1/corsa2R.gif"));  //7
		uno.add(t.getImage("images/nemico1/corsa3R.gif"));  //8
		uno.add(t.getImage("images/nemico1/corsa2R.gif"));  //9
		
		uno.add(t.getImage("images/nemico1/salto.gif"));  //10
		uno.add(t.getImage("images/nemico1/saltoR.gif"));  //11
		
		uno.add(t.getImage("images/nemico1/morto1.gif"));  //11
		uno.add(t.getImage("images/nemico1/morto2.gif"));  //12
		uno.add(t.getImage("images/nemico1/morto3.gif"));  //13
		uno.add(t.getImage("images/nemico1/morto4.gif"));  //14
		uno.add(t.getImage("images/nemico1/morto5.gif"));  //15
		uno.add(t.getImage("images/nemico1/morto6.gif"));  //16
		uno.add(t.getImage("images/nemico1/morto7.gif"));  //17		
		
		due = new Vector<Image>();
		due.add(t.getImage("images/nemico2/fermo.gif"));  //0
		due.add(t.getImage("images/nemico2/corsa1.gif"));  //1
		due.add(t.getImage("images/nemico2/corsa2.gif"));  //2
		due.add(t.getImage("images/nemico2/corsa3.gif"));  //3
		due.add(t.getImage("images/nemico2/corsa2.gif"));  //4
		
		due.add(t.getImage("images/nemico2/fermoR.gif"));  //5
		due.add(t.getImage("images/nemico2/corsa1R.gif"));  //6
		due.add(t.getImage("images/nemico2/corsa2R.gif"));  //7
		due.add(t.getImage("images/nemico2/corsa3R.gif"));  //8
		due.add(t.getImage("images/nemico2/corsa2R.gif"));  //9
		
		due.add(t.getImage("images/nemico2/fermo.gif"));  //10
		due.add(t.getImage("images/nemico2/fermoR.gif"));  //11
		
		due.add(t.getImage("images/nemico2/morto1.gif"));  //11
		due.add(t.getImage("images/nemico2/morto2.gif"));  //12
		due.add(t.getImage("images/nemico2/morto3.gif"));  //13
		due.add(t.getImage("images/nemico2/morto4.gif"));  //14
		due.add(t.getImage("images/nemico2/morto5.gif"));  //15
		due.add(t.getImage("images/nemico2/morto6.gif"));  //16
		due.add(t.getImage("images/nemico2/morto7.gif"));  //17		
	}
	
	public int getFirstTile(int indicePlayer) {
		int offX = this.players.get(indicePlayer).getOffSetX();
		return (offX+(0) - (Myframe.SCREEN_WIDTH/2)) / Livello.TILE_SIZE;
		
	}
	
	public int getLastTile(int indicePlayer) {
		int offX = this.players.get(indicePlayer).getOffSetX();
		return (offX+(0) + (Myframe.SCREEN_WIDTH/2)) / Livello.TILE_SIZE;
		
	}
	
	public int getModulo(int indicePlayer) {
		int offX = this.players.get(indicePlayer).getOffSetX();
		return (offX+(0) - (Myframe.SCREEN_WIDTH/2)) % Livello.TILE_SIZE;
		
	}
	
	public Sprite[][] getMappa() {
		return mappa;
	}
	
	public Sprite getMappaElement(int i, int j) {
		return mappa[i][j];
	}

	public int getHeight() {
		return height;
	}

	public void setHeight(int height) {
		this.height = height;
	}

	public int getWidth() {
		return width;
	}

	public void setWidth(int width) {
		this.width = width;
	}

	public ArrayList<Giocatore> getPlayers() {
		return players;
	}
	
	public Giocatore getPlayerAt(int i) {
		return players.get(i);
	}
	
	public Nemico getNemicoAt(int i) {
		return nemici.get(i);
	}

	public ArrayList<Nemico> getNemici() {
		return nemici;
	}
	

	//metodi per la lettura del file
	
	private void leggiFile(){
		try{
			File file = new File(this.nomeFile);
			FileReader read = new FileReader(file);
			BufferedReader in = new BufferedReader (read);
			String linea;
			while ( (linea=in.readLine())!= null){
				this.listaLinee.add(linea);
			}
			in.close();
		}
		catch (FileNotFoundException e){
			System.out.println(e);
		}
		catch (IOException e){
			System.out.println(e);
		}
	}
	
	private void inizializzaMatrice(int w,int h){
		this.mappa = new Sprite[w][h];
	}
	
	private boolean isComment (String s){
		if (s.startsWith("#")){
			return true;
		}
		return false;
	}
	
	private boolean isDimension (String s){
		if (s.startsWith("%")){
			return true;
		}			
		return false;
	}
	
	private boolean isColor (String s){
		if (s.startsWith("color:")){
			return true;
		}			
		return false;
	}
	
	public void creaMappa(){
		int k=0;
		for (int i=0; i<this.listaLinee.size(); i++){
			String riga = listaLinee.get(i);
			if (isDimension(riga)){
				String[] s = riga.split("%");
				this.width = Integer.valueOf(s[1]).intValue();
				this.height = Integer.valueOf(s[2]).intValue();
				//System.out.println("Width: "+width+ " Height: "+height);
				this.inizializzaMatrice(height,width);
			}
			else if (isColor(riga)){
				String[] s2 = riga.split("color:");
				this.background = new Color(Integer.valueOf(s2[1]).intValue());
			}
			else if (riga.startsWith("background:")) {
				String[] s2 = riga.split("background:");
				this.imgBackground = t.getImage("images/sfondi/"+s2[1]);				
			}
			else if (riga.startsWith("sound:")) {
				String[] s2 = riga.split("sound:");
				this.sottofondo.addSuoni(s2[1]);		
			}
			else if (riga.startsWith("tileset:")) {
				String[] s2 = riga.split("tileset:");
				this.tileset = s2[1];		
				System.out.println(this.tileset);
			}
			else if(isComment(riga)) {}
			/*
			else if (riga.startsWith("soundtrack:")) {
				String[] s2 = riga.split("soundtrack:");
				this.imgBackground = t.getImage("images/sfondi/"+s2[1]);				
			}
			*/
			
			else {
				String [] str1 = riga.split("");
				for (int j=1; j <str1.length; j++){
					creaSprite(str1[j],k,j-1);
				}
				k++;
			}
			
		}
	}
	
	public void creaSprite(String s,int i, int j) {
		switch(s.charAt(0)) {
		 case 'A':
			 this.mappa[i][j] = new Mattone(0,0,t.getImage("images/tile/"+tileset+"/tile_A.png")); 
			 break;
		 case 'B':
			 this.mappa[i][j] = new Mattone(0,0,t.getImage("images/tile/"+tileset+"/tile_B.png")); 
			 break;
		 case 'C':
			 this.mappa[i][j] = new Mattone(0,0,t.getImage("images/tile/"+tileset+"/tile_C.png")); 
			 break;
		 case 'D':
			 this.mappa[i][j] = new Mattone(0,0,t.getImage("images/tile/"+tileset+"/tile_D.png")); 
			 break;
		 case 'E':
			 this.mappa[i][j] = new Mattone(0,0,t.getImage("images/tile/"+tileset+"/tile_E.png")); 
			 break;
		 case 'F':
			 this.mappa[i][j] = new Mattone(0,0,t.getImage("images/tile/"+tileset+"/tile_F.png")); 
			 break;
		 case 'G':
			 this.mappa[i][j] = new Mattone(0,0,t.getImage("images/tile/"+tileset+"/tile_G.png")); 
			 break;
		 case 'H':
			 this.mappa[i][j] = new Mattone(0,0,t.getImage("images/tile/"+tileset+"/tile_H.png")); 
			 break;
		 case '@':
			 this.mappa[i][j] = new Star(0,0);
			 break;
		 case '!':
			 this.mappa[i][j] = new Energy(0,0);
			 break;
		 case '?':
			 this.mappa[i][j] = new LevelUp(0,0);
			 break;
		 case '$':
			 this.mappa[i][j] = new Life(0,0);
			 break;
		 case '1':
			 this.nemici.add(new Nemico(j*Livello.TILE_SIZE,-5,i*Livello.TILE_SIZE,width*Livello.TILE_SIZE,uno,mappa,players.get(0),2,5));
			 this.mappa[i][j] = null;
			 break;
		 case '2':
			 this.nemici.add(new Nemico(j*Livello.TILE_SIZE,-5,i*Livello.TILE_SIZE,width*Livello.TILE_SIZE,due,mappa,players.get(0),4,1));
			 this.mappa[i][j] = null;
			 break;
		 case ' ':
			 this.mappa[i][j] = null;
			 break;
		}
	}
	
	 public Color getBackground() {
			return background;
	 }

	public int getIdLivello() {
		return idLivello;
	}

	public void setIdLivello(int idLivello) {
		this.idLivello = idLivello;
	}

	public Image getImgBackground() {
		return imgBackground;
	}
	public void setMappaElement(int i, int j,Sprite s) {
		mappa[i][j] = s;
	}

	public void setImgBackground(Image imgBackground) {
		this.imgBackground = imgBackground;
	}
	//per la rete
	public int addPlayer() {
		Vector<Image> pp = new Vector<Image>();
		pp.add(t.getImage("images/player/mega1.gif"));  //0
		pp.add(t.getImage("images/player/corsa1.gif")); //1
		pp.add(t.getImage("images/player/corsa2.gif")); //2
		pp.add(t.getImage("images/player/corsa3.gif")); //3
		pp.add(t.getImage("images/player/corsa2.gif")); //4	
		
		pp.add(t.getImage("images/player/mega1R.gif"));  //5
		pp.add(t.getImage("images/player/corsa1R.gif")); //6
		pp.add(t.getImage("images/player/corsa2R.gif")); //7
		pp.add(t.getImage("images/player/corsa3R.gif")); //8
		pp.add(t.getImage("images/player/corsa2R.gif")); //9
		
		pp.add(t.getImage("images/player/salto.gif")); //10
		pp.add(t.getImage("images/player/saltoR.gif")); //11
		
		pp.add(t.getImage("images/player/sparo.gif")); //12
		pp.add(t.getImage("images/player/sparo1.gif")); //13
		pp.add(t.getImage("images/player/sparo2.gif")); //14
		pp.add(t.getImage("images/player/sparo3.gif")); //15
		pp.add(t.getImage("images/player/sparo2.gif")); //16
		
		pp.add(t.getImage("images/player/sparoR.gif")); //17
		pp.add(t.getImage("images/player/sparo1R.gif")); //18
		pp.add(t.getImage("images/player/sparo2R.gif")); //19
		pp.add(t.getImage("images/player/sparo3R.gif")); //20
		pp.add(t.getImage("images/player/sparo2R.gif")); //21
		
		pp.add(t.getImage("images/player/morto1.gif")); //22
		pp.add(t.getImage("images/player/morto2.gif")); //23
		pp.add(t.getImage("images/player/morto3.gif")); //24
		pp.add(t.getImage("images/player/morto4.gif")); //25
		pp.add(t.getImage("images/player/morto5.gif")); //26

		pp.add(t.getImage("images/player/morto1R.gif")); //27
		pp.add(t.getImage("images/player/morto2R.gif")); //28
		pp.add(t.getImage("images/player/morto3R.gif")); //29
		pp.add(t.getImage("images/player/morto4R.gif")); //30
		pp.add(t.getImage("images/player/morto5R.gif")); //31
		
		players.add(new Giocatore(4*50,9*50,width*Livello.TILE_SIZE,pp,mappa));
		return players.size()-1;
	}

	public Sound getSottofondo() {
		return sottofondo;
	}

	public void setSottofondo(Sound sottofondo) {
		this.sottofondo = sottofondo;
	}

	public boolean isType() {
		return type;
	}
	 
	 


}
